package com.agv.backend.controller;

import com.agv.backend.entity.pojo.AgvTask;
import com.agv.backend.entity.AjaxResult;
import com.agv.backend.entity.PageParam;
import com.agv.backend.entity.TableDataInfo;
import com.agv.backend.entity.vo.AgvUploadInfoVO;
import com.agv.backend.service.AgvTaskService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/agv/task")
public class AgvTaskController {

    private static final Logger log = LoggerFactory.getLogger(AgvTaskController.class);
    private final AgvTaskService agvTaskService;

    @Autowired
    public AgvTaskController(AgvTaskService agvTaskService) {
        this.agvTaskService = agvTaskService;
    }

    @GetMapping("/list")
    public TableDataInfo listTask(AgvTask task, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AgvTask> tasks = agvTaskService.selectAgvTaskList(task);
        PageInfo<AgvTask> pageInfo = new PageInfo<>(tasks);
        return TableDataInfo.of(pageInfo.getTotal(), tasks, "200", "查询成功");
    }


    @GetMapping("/{id}")
    public AjaxResult getTask(@PathVariable("id") Long id){
        return AjaxResult.success(agvTaskService.selectAgvTaskById(id));
    }

    @PostMapping
    public AjaxResult addTask(@RequestBody AgvTask task){
        return AjaxResult.success(agvTaskService.insertAgvTask(task));
    }

    @PutMapping
    public AjaxResult updateTask(@RequestBody AgvTask task){
        return AjaxResult.success(agvTaskService.updateAgvTask(task));
    }

    @DeleteMapping("/{id}")
    public AjaxResult delTask(@PathVariable("id") Long id){
        int row = agvTaskService.deleteAgvTaskById(id);
        return row > 0 ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    @PostMapping("/start/{id}")
    public AjaxResult startTask(@PathVariable("id") Long id) throws Exception {
        return agvTaskService.startTask(id);
    }

    @PostMapping("/end/{id}")
    public AjaxResult endTask(@PathVariable("id") Long id, @RequestParam("isAbort") boolean isAbort) throws Exception{
        return agvTaskService.endTask(id, isAbort);
    }

    @GetMapping("/preupload/{id}")
    public AjaxResult preUploadTask(@PathVariable("id") Long id) {
        List<AgvUploadInfoVO> infoVOList = agvTaskService.preUploadTask(id);
        return AjaxResult.success(infoVOList);
    }

    @PostMapping("/upload/{id}")
    public AjaxResult uploadTask(@PathVariable("id") Long id) {
        int result = agvTaskService.uploadTask(id);
        return AjaxResult.success(result);
    }

    @GetMapping("/time")
    public TableDataInfo getTaskByTime(@RequestParam String startTime,@RequestParam String endTime,@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<AgvTask> list = agvTaskService.getTasksByTime(startTime, endTime);
        return TableDataInfo.of(new PageInfo(list).getTotal(), list, "200", "查询成功");
    }


}
